#include "stdafx.h"
#include "StateManager.h"
#include "../Game.h"
#include "BaseState.h"
#include "GameplayState.h"

StateManager::StateManager()
{
	game = nullptr;
}

StateManager::~StateManager()
{

}

void StateManager::Initialize(Game * game)
{
	this->game = game;

	GameplayState * gameplayState = new GameplayState;
	gameplayState->Initialize(game);
	PushState(gameplayState);
}

void StateManager::Shutdown(void)
{
	ClearStack();
}

bool StateManager::Input(void)
{
	if (stack.empty())
		return true;

	return stack.back()->Input();
}

void StateManager::Update(float elapsedTime)
{
	for (unsigned int index = 0; index < stack.size(); index++)
		stack[index]->Update(elapsedTime);
}

void StateManager::PushState(BaseState * state)
{
	stack.push_back(state);
	stack.back()->Enter();
}

void StateManager::PopState(void)
{
	stack.back()->Exit();
	SafeDelete(stack.back());
	stack.pop_back();
}

void StateManager::ChangeState(BaseState * state)
{
	PopState();
	PushState(state);
}

void StateManager::ClearStack(void)
{
	while (!stack.empty())
		PopState();
}